package vsvm.graphics.charts;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import vsvm.classifier.AbstractTestSet;
import vsvm.classifier.TestCase;

public class ProbabilityChart extends AbstractChart {
	private static final long serialVersionUID = 100L;

	private static final int DS_SEPARETOR    = 0;
	private static final int DS_POSITIVE_CLASS  = 1;
	private static final int DS_NEGATIVE_CLASS  = 2;
	private static final int DS_MISSCLASSIFIED_CLASS = 3;
	
	Iterator <TestCase> testCases = null;
	AbstractTestSet testSet = null;
	Random random = new Random(10);
	
	public ProbabilityChart(AbstractTestSet testSet, String title) {
		super(title);
		this.testSet = testSet;
		testCases = testSet.getTestCases();
		
	}
	
	protected Graphics2D drawChart(Graphics2D g) {
		// TODO Auto-generated method stub
		
		return null;
	}
	
	protected void prepareColors() {		
		this.setColors(new Color[]{Color.BLACK, Color.GREEN, Color.BLUE, Color.RED});
	}
	
	public void prepareChart() {
		this.setXRange(0.0, 1.0);
		this.setYRange(0.0, 1.0);
		this.setTitle(title);
		
		
		//Add separator
		this.addPoint(this.DS_SEPARETOR, 0, 0.5, true);
		this.addPoint(0, 1, 0.5, true);		
		
		this.setMarksStyle("Dots");
		this.setMarksStyle("none", 0);
		
		this.setGrid(false);
		
		double positive = this.testSet.getPositiveClass();
		double negative = this.testSet.getNegativeClass();		
		
		while (testCases.hasNext()) {
			TestCase current = testCases.next();
			
			double [] prob = current.getProbabilities();
			double ac = current.getActualClass();
			double pc = current.getPredictedClass();			
			if (Math.abs(pc - ac) > 1e-8) {
				this.addPoint(this.DS_MISSCLASSIFIED_CLASS, random.nextDouble(), prob[1], false);
			} else if (Math.abs(ac - positive) < 1e-8){
				this.addPoint(this.DS_POSITIVE_CLASS, random.nextDouble(), prob[1], false);
			} else 
				this.addPoint(this.DS_NEGATIVE_CLASS, random.nextDouble(), prob[1], false);
			
		}
		
		
		this.addLegend(DS_MISSCLASSIFIED_CLASS, "Missclassified");
		this.addLegend(DS_POSITIVE_CLASS,  "Positive Class");
		this.addLegend(DS_NEGATIVE_CLASS,  "Negative Class");
		this.addXTick("", 0.5);
		
		this.setYLabel("Positive class probability");
		
		this.chartPrepared = true;		
	}

}
